package com.cloudkinto.controller.tailtrucks;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.DicBusinessItemConstant;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.constant.SysConstant;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.fbaorderoutput.FbaOrderOutputService;
import com.cloudkinto.service.fbaorderoutput.vo.FbaOrderOutputUploadFileReq;
import com.cloudkinto.service.firstvesselorder.FirstVesselOrderService;
import com.cloudkinto.service.orderoutput.vo.OrderOutputListRes;
import com.cloudkinto.service.tailtrucks.TailTrucksService;
import com.cloudkinto.service.tailtrucks.vo.*;
import io.swagger.annotations.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhangyy
 * @since 2020-10-28
 */
@RestController
@Api(tags = "尾程约车模块")
@RequestMapping("/api/admin/tailTrucks")
public class TailTrucksController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(TailTrucksController.class);

    @Autowired
    private TailTrucksService service;
    @Autowired
    private FirstVesselOrderService firstVesselOrderService;
    @Autowired
    private FbaOrderOutputService fbaOrderOutputService;

    @GetMapping("/addInit")
    @ApiOperation(value = "新增初始化", notes = "新增初始化")
    public SingleResult addInit() {
        return service.addInit();
    }

    @UserSessionRequired
    @PostMapping("")
    @ApiOperation(value = "新增", notes = "新增")
    public SingleResult add(@RequestBody @Validated TailTrucksAddReq req) {
        service.add(req, getCurrentUserId());
        return SingleResult.success();
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit(getCompanyId(), getCurrentUserId());
    }


    @UserSessionRequired
    @PostMapping("amazonTime")
    @ApiOperation(value = "亚马逊ID", notes = "亚马逊ID")
    public SingleResult amazonTime(@RequestBody @Validated TailTrucksConTimeReq req) {
        service.amazonTime(req, getCurrentUserId());
        return SingleResult.success();
    }

    @UserSessionRequired
    @PostMapping("trucksAppointment")
    @ApiOperation(value = "预约车辆", notes = "新增")
    public SingleResult trucksAppointment(@RequestBody @Validated TailTrucksAppointmentReq req) {
        service.trucksAppointment(req, null);
        return SingleResult.success();
    }

    @UserSessionRequired
    @PostMapping("autoTrucksAppointment")
    @ApiOperation(value = "自动拼单", notes = "自动拼单")
    public SingleResult autoTrucksAppointment(@RequestBody @Validated @NotEmpty(message = "pls.choose.order") List<Long> ids) {
        service.autoTrucksAppointment(ids);
        return SingleResult.success();
    }

    @UserSessionRequired
    @GetMapping("/updateStatusInit")
    @ApiOperation(value = "编辑状态初始化", notes = "新增初始化")
    public SingleResult updateStatusInit() {
        return service.updateStatusInit();
    }

    @UserSessionRequired
    @PostMapping("/updateStatus")
    @ApiOperation(value = "编辑状态", notes = "新增初始化")
    public SingleResult updateStatus(@RequestBody @Validated TailTrucksUpdateStatusReq req) {
        service.updateStatus(req, getCurrentUserId());
        return SingleResult.success();
    }

    @UserSessionRequired
    @PostMapping("/order/uploadSignatureFile")
    @ApiOperation(value = "签收单回传", notes = "截单")
    public SingleResult uploadSignatureFile(@RequestBody @Validated FbaOrderOutputUploadFileReq req) {
        Long userId = getCurrentUserId();
        service.uploadSignatureFile(req);
        return SingleResult.success("上传成功", null);
    }

    @UserSessionRequired
    @PostMapping("jointTrucks")
    @ApiOperation(value = "拼车", notes = "拼车")
    public SingleResult jointTrucks(@RequestBody @Validated TailTrucksJointTrucksReq req) {
        service.jointTrucks(req, true);
        return SingleResult.success();
    }

//    @GetMapping("/updateInit/{id}")
//    @ApiOperation(value = "编辑初始化", notes = "编辑初始化")
//    public SingleResult updateInit(@PathVariable("id") Long id) {
//        return service.updateInit(id);
//    }
//
//    @PutMapping("")
//    @ApiOperation(value = "修改", notes = "修改")
//    public SingleResult update(@RequestBody @Validated Object req) {
//        Long userId = getCurrentUserId();
//        return SingleResult.success("修改成功",service.update(req,userId));
//    }

    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        return SingleResult.success("删除成功", service.delete(id, getCurrentUserId()));
    }

    @UserSessionRequired
    @PostMapping("deleteOrder")
    @ApiOperation(value = "删除单个约车的订单", notes = "根据id删除")
    public SingleResult deleteOrder(@RequestBody TailTrucksDeleteReq req) {
        return SingleResult.success("删除成功", service.deleteOrder(req));
    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", service.detail(id));
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "status", value = "状态"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "keyword", value = "关键字（模糊查询）"),
            @ApiImplicitParam(name = "startDate", value = "开始时间"),
            @ApiImplicitParam(name = "endDate", value = "结束时间"),
            @ApiImplicitParam(name = "truckId", value = "车型"),
            @ApiImplicitParam(name = "fbaStorageId", value = "FBA仓库id"),
            @ApiImplicitParam(name = "storageId", value = "仓库id"),
            @ApiImplicitParam(name = "companyCode", value = "公司标识")})
    public PageResult listPage(HttpServletRequest request) {

        Map<String, String> map = new HashMap<>();
        String status = request.getParameter("status");
        String keyword = request.getParameter("keyword");
        String current = request.getParameter("current");
        String pageSize = request.getParameter("pageSize");
        String fbaStorageId = request.getParameter("fbaStorageId");
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        String trackId = request.getParameter("truckId");
        String storageId = request.getParameter("storageId");
        String companyCode = request.getParameter("companyCode");
        map.put("status", status);
        map.put("keyword", keyword);
        map.put("current", current);
        map.put("pageSize", pageSize);
        map.put("keyword", keyword);
        map.put("current", current);
        map.put("pageSize", pageSize);
        map.put("status", status);
        map.put("fbaStorageId", fbaStorageId);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        map.put("truckId", trackId);
        map.put("storageId", storageId);
//        map.put("shopId", shopId);
//        map.put("orderType", "2");//fba转运
        if (companyCode != null) {
            map.put("companyCode", companyCode + "");
        }
        return service.getListPage(map);
    }

    @UserSessionRequired
    @GetMapping("/FirstVesselOrderListPage")
    @ApiOperation(value = "未约车的头程订单查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "fbaStorageId", value = "fba仓库id"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "keyword", value = "关键字（模糊查询）"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")})
    public PageResult FirstVesselOrderListPage(HttpServletRequest request) {

        Map<String, String> map = new HashMap<>();
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        String storageId = request.getParameter("storageId");
        String fbaStorageId = request.getParameter("fbaStorageId");
        map.put("is_container", SysConstant.DELETE_FLAG_FALSE.toString());
        map.put("storageId", storageId);
        map.put("fbaStorageId", fbaStorageId);
        map.put("current", current);
        map.put("pageSize", size);
        return firstVesselOrderService.getListPage(map);
    }

    @UserSessionRequired
    @GetMapping("/fbaOrderlistPage")
    @ApiOperation(value = "未约车的转运订单查询查询（分页）", notes = "查询（分页）")
    @ApiResponses({@ApiResponse(code = 200, message = "", response = OrderOutputListRes.class)})
    public PageResult<OrderOutputListRes> listPage(String keyword, String current, String pageSize, String storageId, String fbaStorageId) {
        Map<String, String> map = new HashMap<>();
        map.put("keyword", keyword);
        map.put("current", current);
        map.put("pageSize", pageSize);
        if (storageId.equals("1")||storageId.equals("10")||storageId.equals("11")) {
            map.put("storageId1", "1");
            map.put("storageId2", "10");
            map.put("storageId3", "11");
        }else {
            map.put("storageId", storageId);
        }
        map.put("fbaStorageId", fbaStorageId);
        map.put("isSendOut", DicBusinessItemConstant.fbaOrderSendOutStatus + "");//fba转运未发货订单
        map.put("isTrucks", SysConstant.DELETE_FLAG_FALSE.toString());
        return fbaOrderOutputService.getListPage(map);
    }

    @UserSessionRequired
    @GetMapping("/exportAmazonReceivePage")
    @ApiOperation(value = "导出受领书", notes = "导出受领书")
    public void exportAmazonReceivePage(Long id, HttpServletResponse response) {
        service.exportAmazonReceivePage(id, response);
    }

    @UserSessionRequired
    @GetMapping("/exportPickupPage")
    @ApiOperation(value = "导出拣货单", notes = "导出拣货单")
    public void exportPickupPage(Long id, HttpServletResponse response) {
        service.exportPickupPage(id, response);
    }


    @UserSessionRequired
    @PostMapping("updateAmazonTime")
    @ApiOperation(value = "修改亚马逊确认时间", notes = "修改亚马逊确认时间")
    public SingleResult updateAmazonTime(@RequestBody @Validated UpdateAmazonTimeReq req) {
        service.updateAmazonTime(req);
        return SingleResult.success();
    }
}

